/**
 * Project: ld.biz.web
 * 
 * File Created at 2012-9-26
 * $Id$
 * 
 * Copyright 1999-2100 Alibaba.com Corporation Limited.
 * All rights reserved.
 *
 * This software is the confidential and proprietary information of
 * Alibaba Company. ("Confidential Information").  You shall not
 * disclose such Confidential Information and shall use it only in
 * accordance with the terms of the license agreement you entered into
 * with Alibaba.com.
 */
package com.liangdev.web.action.autocode;

import java.io.IOException;

import javax.annotation.Resource;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.liangdev.biz.core.autocode.AutoCodeFactory;
import com.liangdev.biz.entity.autocode.AutoCodeFile;
import com.liangdev.biz.web.util.ActionUtil;
import com.liangdev.common.util.StringUtil;
import com.opensymphony.xwork2.ActionSupport;

/**
 * TODO Comment of ExecuteAction
 * 
 * @author chongan.wangca
 */
public class ExecuteAction extends ActionSupport {

    /**
     * 
     */
    private static final long serialVersionUID = 2022616567518170963L;

    private final Logger      logger           = LoggerFactory.getLogger(ExecuteAction.class);

    @Resource
    private ActionUtil        actionUtil;

    @Resource
    private AutoCodeFactory   autoCodeFactory;

    public void getTabsInfoJson() {
        String tableRecords = actionUtil.getRequestParamString("tableRecords");
        String templateRecords = actionUtil.getRequestParamString("templateRecords");

        //        [{
        //            title: 'Ajax Tab 2',
        //            loader: {
        //                url: 'ajax2.htm',
        //                contentType: 'html',
        //                autoLoad: true,
        //                params: 'foo=123&bar=abc'
        //            }
        //        }]

        StringBuffer json = new StringBuffer();
        json.append("[");
        //0-dbInfoId 1-tableName
        String[] tableRecordsArray = tableRecords.split(",");
        //        int activeCount = 0;
        for (String tableRecord : tableRecordsArray) {
            if (StringUtil.isBlank(tableRecord)) {
                continue;
            }
            String[] tableDetail = tableRecord.split("\\|");

            //0-templataId 1-templateName
            String[] templateRecordsArray = templateRecords.split(",");

            for (String templateRecord : templateRecordsArray) {

                if (StringUtil.isBlank(templateRecord)) {
                    continue;
                }

                String[] templateDetail = templateRecord.split("\\|");

                //参数
                String params = "dbId=" + tableDetail[0] + "&tableName=" + tableDetail[1]
                        + "&templateId=" + templateDetail[0];

                json.append("{");
                json.append("wheelIncrement: 400,");
                json.append("scrollIncrement: 200,");
                json.append("closable: true,");
                json.append("enableTabScroll: true,");
                json.append("title:");
                json.append("'" + tableDetail[1].toLowerCase() + "#" + templateDetail[1] + "',");
                json.append("tooltip:");
                json.append("'" + tableDetail[1].toLowerCase() + "#" + templateDetail[1] + "',");
                json.append("autoScroll: true,");
                //                if (activeCount == 0) {
                //                    json.append("active:true,");
                //                    activeCount++;
                //                }
                json.append("loader: {");//load start
                json.append("url:");
                json.append("'autocode/renderTemplate.json?" + params + "',");
                json.append("contentType:'text',");
                json.append("autoLoad: true,");
                json.append("params: '" + params + "'");
                json.append("}"); //load end
                json.append("}");
                json.append(",");

            }
        }
        //去除最后的多余逗号
        json = new StringBuffer(json.substring(0, json.length() - 1));
        json.append("]");

        try {
            actionUtil.getResponse().getWriter().print(json.toString());
        } catch (IOException e) {
            logger.error("", e);
        }
    }

    public void renderTemplate() {
        Long dbInfoId = actionUtil.getRequestParamLong("dbId");
        String tableName = actionUtil.getRequestParamString("tableName");
        Long templateInfoId = actionUtil.getRequestParamLong("templateId");
        try {
            AutoCodeFile autoCodeFile = autoCodeFactory.generate(dbInfoId, templateInfoId,
                    tableName);
            String content = autoCodeFile.getContent();
            actionUtil.getResponse().getWriter().print(content);
        } catch (Exception e1) {
            e1.printStackTrace();
        }
    }
}
